//
//  LZUserInfo.m
//  DoctorFAQs
//
//  Created by langZai on 2017/2/23.
//  Copyright © 2017年 zjq. All rights reserved.
//

#import "LZUserInfo.h"

@implementation LZUserInfo

static LZUserInfo *model = nil;


+ (instancetype)shareInfo {
    static dispatch_once_t onceToken;
    dispatch_once(&onceToken, ^{
        model = [[LZUserInfo alloc] init];
    });
    return model;
}


+ (instancetype)allocWithZone:(struct _NSZone *)zone {
    static dispatch_once_t onceToken;
    dispatch_once(&onceToken, ^{
        model = [super allocWithZone:zone];
    });
    return model;
}

//归档
- (void)encodeWithCoder:(NSCoder *)aCoder {
    [aCoder encodeBool:_isLogin forKey:@"isLogin"];
    [aCoder encodeObject:_uid forKey:@"uid"];
    [aCoder encodeObject:_phone forKey:@"phone"];
    [aCoder encodeObject:_passWord forKey:@"passWord"];
    [aCoder encodeObject:_userName forKey:@"userName"];
    [aCoder encodeObject:_userIcon forKey:@"userIcon"];
    [aCoder encodeObject:_userAge forKey:@"userAge"];
    [aCoder encodeObject:_userSex forKey:@"uaerSex"];
    [aCoder encodeObject:_userTime forKey:@"userTime"];
    
}
//解归档
- (nullable instancetype)initWithCoder:(NSCoder *)aDecoder
{
    if (self = [super init]) {
        _isLogin = [aDecoder decodeBoolForKey:@"isLogin"];
        _phone = [aDecoder decodeObjectForKey:@"phone"];
        _passWord = [aDecoder decodeObjectForKey:@"passWord"];
        _userName = [aDecoder decodeObjectForKey:@"userName"];
        _userIcon = [aDecoder decodeObjectForKey:@"userIcon"];
        _userAge = [aDecoder decodeObjectForKey:@"userAge"];
        _userSex = [aDecoder decodeObjectForKey:@"uaerSex"];
        _userTime = [aDecoder decodeObjectForKey:@"userTime"];
        _uid = [aDecoder decodeObjectForKey:@"uid"];
    }
    
    return self;
}

// 保存模型
+ (void)saveUserInfo:(LZUserInfo *)model {
    //将模型装换成数据类型
    NSData *data = [NSKeyedArchiver archivedDataWithRootObject:model];
    //存储数据
    [[NSUserDefaults standardUserDefaults]setObject:data forKey:@"USERINFO"];
    //同步
    [[NSUserDefaults standardUserDefaults]synchronize];
}

// 得到用户信息模型
//+ (LZUserInfo *)getUserInfo {
//    LZUserInfo *model = [LZUserInfo shareInfo];
//    NSData *data = [[NSUserDefaults standardUserDefaults]objectForKey:@"USERINFO"];
//    if ( [NSKeyedUnarchiver unarchiveObjectWithData:data]) {
//        model = [NSKeyedUnarchiver unarchiveObjectWithData:data];
//        
//    };
//    NSLog(@"%@-- %@--%@",[LZUserInfo shareInfo].uid,[LZUserInfo shareInfo].userTime,[LZUserInfo shareInfo].userName);
//        return model;
//}
+ (LZUserInfo *)getUserInfo {
    LZUserInfo *model = [LZUserInfo shareInfo];
    NSData *data = [[NSUserDefaults standardUserDefaults]objectForKey:@"USERINFO"];
    if ( [NSKeyedUnarchiver unarchiveObjectWithData:data]) {
        model = [NSKeyedUnarchiver unarchiveObjectWithData:data];
       
    };
    return model;
}
// 移除所有用户信息
+ (void)deleteUserInfo {
//    [[NSUserDefaults standardUserDefaults] removeObjectForKey:@"USERINFO"];
    [LZUserInfo shareInfo].uid = @"";
    [LZUserInfo shareInfo].userName = @"";
    [LZUserInfo shareInfo].phone = @"";
    [LZUserInfo shareInfo].userAge = @"";
    [LZUserInfo shareInfo].userSex = @"";
    [LZUserInfo shareInfo].userIcon = @"";
    [LZUserInfo shareInfo].userTime = @"";
    [LZUserInfo shareInfo].passWord = @"";
    [LZUserInfo saveUserInfo:[LZUserInfo shareInfo]];
}


@end
